import time

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

from common.Utils import pilotDriver
class PilotLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="https://www.codepilot.fun/user/login"
        self.driver=pilotDriver.driver
        self.driver.get(self.url)
    #成功登录的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#userAccount").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#userPassword").clear()

        self.driver.find_element(By.CSS_SELECTOR,"#userAccount").send_keys("root")
        self.driver.find_element(By.CSS_SELECTOR,"#userPassword").send_keys("root123456")
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR,"#userLoginPage > div > div.ant-pro-form-login-main.css-ccdg5a > form > button > span").click()
        time.sleep(3)
        # 能够找到首页昵称，说明登录成功，否则登录失败
        self.driver.find_element(By.CSS_SELECTOR,"#basicLayout > div > div.ant-layout.css-ccdg5a > div > header > div > div > div.ant-pro-global-header-right-content.css-1mjju6j > div > div > div > span > div > span:nth-child(2)")
        # 添加屏幕截图
        pilotDriver.getScreenShot()
        #返回登陆页面
        #self.driver.back()
        back=self.driver.find_element(By.CSS_SELECTOR,"#basicLayout > div > div.ant-layout.css-ccdg5a > div > header > div > div > div.ant-pro-global-header-right-content.css-1mjju6j > div > div > div > span > div")
        #模拟鼠标悬浮
        actions = ActionChains(self.driver)
        actions.move_to_element(back).perform()
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR,"body > div:nth-child(10) > div > ul > li:nth-child(2) > span.ant-dropdown-menu-title-content").click()
        time.sleep(2)
        print("正常登陆验证完毕")

    #失败登录的测试用例
    def LoginFailTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#userAccount").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#userPassword").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#userAccount").send_keys("root")
        #错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#userPassword").send_keys("root123")
        self.driver.find_element(By.CSS_SELECTOR,"#userLoginPage > div > div.ant-pro-form-login-main.css-ccdg5a > form > button > span").click()
        time.sleep(2)
        #检查是否登录失败
        # actual=self.driver.find_element(By.CSS_SELECTOR,"div > div > div > div > span:nth-child(2)").text
        # print(actual)
        # assert actual=="登录失败，密码错误"
        #添加屏幕截图
        pilotDriver.getScreenShot()
        try:
            self.driver.find_element(By.CSS_SELECTOR,"#basicLayout > div > div.ant-layout.css-ccdg5a > div > header > div > div > div.ant-pro-global-header-right-content.css-1mjju6j > div > div > div > span > div > span:nth-child(2)")
        except:
            print("登录失败，密码错误")
            print("异常登录验证完毕")
        time.sleep(2)

login=PilotLogin()
login.LoginSucTest()

login.LoginFailTest()